package com.suep.business.controller;

import com.suep.business.domain.Meal;
import com.suep.business.domain.Store;
import com.suep.business.service.IMealService;
import com.suep.business.service.IStoreService;
import com.suep.file.service.IFileService;
import com.suep.system.config.BaseR;
import com.suep.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/meal")
public class MealController {
    @Autowired
    private IMealService mealService;
    @Autowired
    private IStoreService storeService;

//    因为涉及到图片文件，修改与更新的时候都要用到它
    @Autowired
    private IFileService fileService;
    /*适用于店铺*/
    @GetMapping("/getMealListByStoreId")
    public BaseR<List<Meal>> getMealListByStoreId(HttpServletRequest request) {
        Integer userId= Integer.parseInt( request.getAttribute("userId").toString());
        Store store = storeService.getStoreByUserId(userId);
        List<Meal> mealList = mealService.selectMealsByStoreId(store.getStoreId());
        return new BaseR<>().ok("获取菜品列表成功",mealList);
    }

    /*适用于学生，根据店铺ID和选中分类来获取菜品列表*/
    @GetMapping("/getMealListByStoreIdByCategoryId")
    public BaseR<List<Meal>> getMealListByStoreId(@RequestParam("storeId") Integer storeId,@RequestParam("categoryId") Integer categoryId) {
        List<Meal> mealList = mealService.selectMealListByStoreIdByCategoryId(storeId,categoryId);
        return new BaseR<>().ok("获取菜品列表成功",mealList);
    }


    @DeleteMapping()
    public BaseR<Integer> removeMealByStoreId(Integer mealId){
        Integer result=mealService.deleteMeal(mealId);
        return new BaseR<>().ok("菜品删除成功",result);

    }

    @PutMapping("/changeStatus")
    public BaseR<Integer> changeMealStatus(@RequestBody Meal meal){
        Integer result=mealService.updateMeal(meal);
        return new BaseR<>().ok("更新菜品状态",result);
    }

    @PutMapping
    public BaseR<Integer> updateMeal(@ModelAttribute Meal meal,@RequestParam(value = "file",required = false) MultipartFile file,HttpServletRequest request) throws IOException {
        Integer userId=Integer.parseInt(request.getAttribute("userId").toString());
        String url=fileService.saveFile(file,userId);
        meal.setImageUrl(url);
        Integer result=mealService.updateMeal(meal);
        return new BaseR<>().ok("菜品更新成功",result);
    }
    @PostMapping
    public BaseR<Integer> addMeal(@ModelAttribute Meal meal,@RequestParam(value = "file",required = false) MultipartFile file,HttpServletRequest request) throws IOException{
        Integer userId=Integer.parseInt(request.getAttribute("userId").toString());

        if(file!=null){
            String url=fileService.saveFile(file,userId);
            meal.setImageUrl(url);
        }

        Store store=storeService.getStoreByUserId(userId);
        meal.setStoreId(store.getStoreId());
        Integer result=mealService.insertMeal(meal);
        return new BaseR<>().ok("添加菜品成功",result);
    }
}
